<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 字母数字验证码
 * 
 * 
 * @author yunyun
 *
 */

class Code_lib
{	
	var $CI;
	var $fontPath		= '';					//字体路径
	var $image;
	var $charLen 		= 4; 					//生成几位验证码
	var $arrChr			= array();				//验证码字符
	var $width 			= 100; 					//图片宽
	var $height 		= 30; 					//图片高
	
	var $bgcolor 		= "#FFF3E0"; 			//背景色
	var $showNoisePix 	= true; 				//生成杂点
	var $noiseNumPix 	= 10; 					//生成杂点数量
	var $showNoiseLine 	= true; 				//生成杂线
	var $noiseNumLine 	= 2; 					//生成杂线数量
	var $showBorder 	= true; 				//边框，当杂点、线一起作用的时候，边框容易受干11扰
	var $borderColor 	= "#FFF3E0";
	
	var $image_url		= "/login/images/";		//图片路径，根据不同来设置。
	var $code_type 		= 'num';				//num数字  zm 字母 all 混合
	

	function __construct($param = array())
	{
		if(count($param) >= 1)
		{
			if(isset($param['url']))
			{
				$this->image_url 	= $param['url'];
			}
			if(isset($param['width']))
			{
				$this->width		= $param['width'];
			}
			if(isset($param['height']))
			{
				$this->height		= $param['height'];
			}
			if(isset($param['bgcolor']))
			{
				$this->bgcolor		= $param['bgcolor'];
			}
			if(isset($param['bordercolor']))
			{
				$this->borderColor		= $param['bordercolor'];
			}
			if(isset($param['code_type']))
			{
				$this->code_type		= $param['code_type'];
			}
		}
		$this->CI = & get_instance();
		$this->fontPath = realpath(dirname(__FILE__) . '/fonts/');	//字体文件
		
		if($this->code_type == 'num')
		{
			$this->arrChr = range(0, 9);//纯数字验 证码 
		}
		elseif($this->code_type == 'zm')
		{
			$this->arrChr = range('A', 'Z');//纯字母验证码
		}
		else
		{
			$this->arrChr = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码
		}
		
	}

	/**
	 * 显示验证码
	 *
	 */
	function show()
	{
		
		$this->image = imagecreate($this->width, $this->height);
		$this->back = $this->getColor($this->bgcolor);
		@imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
		$size = $this->width / $this->charLen - 4;
		if ($size > $this->height) {
			$size = $this->height;
		}
		$left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
		$code='';
		for($i = 0; $i < $this->charLen; $i ++) {
			$randKey = rand(0, count($this->arrChr) - 1);
			$randText = $this->arrChr[$randKey];
			$code .= $randText;
			$textColor = @imagecolorallocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
			$font = $this->fontPath . '/' . rand(1, 5) . ".ttf";
			$randsize = rand($size - $size / 10, $size + $size / 10);
			$location = $left + ($i * $size + $size / 10);
			@imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
		}
		
		if ($this->showNoisePix == true) {
			$this->setNoisePix();
		}
		if ($this->showNoiseLine == true) {
			$this->setNoiseLine();
		}
		if ($this->showBorder == true) {
			$this->borderColor = $this->getColor($this->borderColor);
			@imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
		}
		
		//$_SESSION['auth_code'] = $code;
		
		$this->CI->session->set_userdata('auth_code', $code);
		
		@ob_clean();
		header("Content-type: image/png");
		@imagepng($this->image);
		@imagedestroy($this->image);
	}
	

	/**
	 * 显示验证码的JS调用
	 *
	 */
	function showScript()
	{
		
		//显示验证码
		//echo 'var img_src = "/login/images/"; ';
		echo 'var img_src = "'.site_url(trim($this->image_url,'/').'/id_').'"; ';
		echo 'document.writeln("<img id=\"img_authcode\" src=\"" + img_src + Math.random()+"\" style=\"cursor:pointer;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">");';
	}

	/**
	 * 检查验证码是否正确
	 *
	 * @param string $auth_code
	 * @return bool
	 */
	function check($auth_code = null)
	{
		$auth_code = strtoupper($auth_code);
		
		//$session_code = $_SESSION['auth_code'];
				
		$session_code = $this->CI->session->userdata('auth_code');

		if($session_code && $auth_code)
		{ 
			if($session_code == $auth_code) 
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		return false;
	}

	function getColor($color)
	{
		$color = eregi_replace("^#", "", $color);
		$r = $color[0] . $color[1];
		$r = hexdec($r);
		$b = $color[2] . $color[3];
		$b = hexdec($b);
		$g = $color[4] . $color[5];
		$g = hexdec($g);
		$color = imagecolorallocate($this->image, $r, $b, $g);
		return $color;
	}

	function setNoisePix()
	{
		for($i = 0; $i < $this->noiseNumPix; $i ++) {
			$randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
			imagesetpixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
		}
	}

	function setNoiseLine()
	{
		for($i = 0; $i < $this->noiseNumLine; $i ++) {
			$randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
			imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
		}
	}
}
